package com.example.yuejian2.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.yuejian2.R;
import com.example.yuejian2.bean.IsCollection;
import com.example.yuejian2.bean.MyEntity;
import com.example.yuejian2.common.Url;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import butterknife.BindView;
import butterknife.OnClick;

public class MyActivity extends BaseActivity {

    private static final String TAG = "MyActivity";
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.name)
    TextView name;
    Handler handler_activity = new mHandler();
    private String uid;
    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my);
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        context = this;
        uid = SPUtils.getInstance().getInt("uid") + "";
    }




    @OnClick(R.id.setting)
    void setting() {
        ActivityUtils.startActivity(SettingActivity.class);
    }

    @OnClick(R.id.focus)
    void focus() {
        ActivityUtils.startActivity(FocusClubActivity.class);
    }

    @OnClick(R.id.order)
    void order() {
        ActivityUtils.startActivity(OrderActivity.class);
    }

    @OnClick(R.id.send)
    void send() {
        ActivityUtils.startActivity(FTActivity.class);
    }

    private void initData() {

        EasyHttp.get("yuejian6/public/index.php/json/my/index").params("user_id", uid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 1; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }


    // 步骤1：自定义Handler子类（继承Handler类） & 复写handleMessage（）方法
    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    IsCollection isCollection = null;

                    try {
                        isCollection = JSON.parseObject(msg.obj.toString(), IsCollection.class);
                    } catch (Exception e) {
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }

                    if ("0".equals(isCollection.getError())) {
                        ToastUtils.showShort("修改成功");
                        initData();
                    } else {
                        ToastUtils.showShort("修改失败");
                    }
                    break;
                case 1:
                    MyEntity myEntity = null;

                    try {
                        myEntity = JSON.parseObject(msg.obj.toString(), MyEntity.class);
                    } catch (Exception e) {
                        ToastUtils.showShort("获取数据失败");
                    }


                    Glide.with(context).load(Url.imgUrl + myEntity.getData().getUser_pic()).into(img);
                    name.setText(myEntity.getData().getName());
                    break;
            }
        }
    }
}
